package apps.hackstermia.buttonthings;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Ble2Activity extends Activity {
/** Called when the activity is first created. */
    private Button autopairbtn=null;
    private BluetoothDevice bluetoothDevice;
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ble2);


        autopairbtn=(Button) findViewById(R.id.button1);

        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        //检查蓝牙
        if(bluetoothAdapter != null){
            if(!bluetoothAdapter.isEnabled()){
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                //设置蓝牙可见性
                intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,3000);
                this.startActivity(intent);
                //打开蓝牙
                bluetoothAdapter.enable();
            }
        }else {
            Toast.makeText(this,"本地设备驱动异常",Toast.LENGTH_LONG).show();
        }
        autopairbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //扫描设备
                bluetoothAdapter.startDiscovery();
                //注册广播
                RegisterBluetoothReceiver();
            }
        });
    }
    //注册广播
    public void RegisterBluetoothReceiver(){
        // 设置广播信息过滤
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
        // 注册广播接收器，接收并处理搜索结果
        registerReceiver(BTReceive, intentFilter);
    }
    //处理蓝夜绑定的广播
    private BroadcastReceiver BTReceive = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
                bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String bluetoothName;
                if((bluetoothName = bluetoothDevice.getName()) != null){
                    // 如果查找到的设备符合要连接的设备，处理
                    if (bluetoothName.equals("lmln_bale_car")) {
                        //搜索蓝牙占用资源，搜索到后要及时关闭
                        bluetoothAdapter.cancelDiscovery();
                        //获取蓝牙的连接状态
                        int connectState = bluetoothDevice.getBondState();
                        switch (connectState){
                            //未配对
                            case BluetoothDevice.BOND_NONE:
                                //开始配对
                                try{
                                    ClsUtils.createBond(bluetoothDevice.getClass(),bluetoothDevice);
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                                break;
                        }
                    }
                }
            }else if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
                try {
                    //如果不结束广播接收，配对界面会闪出
                    abortBroadcast();
                    //顺序一定要这样，否则会出问题
                    ClsUtils.setPin(bluetoothDevice.getClass(), bluetoothDevice, "1234");
                    //这行代码会在控制台报错
//                    ClsUtils.setPairingConfirmation(bluetoothDevice.getClass(), bluetoothDevice,true);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销广播
        unregisterReceiver(BTReceive);
    }

}
